//
//  LWStatusOriginalView.swift
//  LW_Weibo
//
//  Created by 刘伟 on 16/7/13.
//  Copyright © 2016年 LWSpro. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

//原创微博
class LWStatusOriginalView: UIView {

    
    //记录当前视图的底部约束
    var originalViewBottomConstraint: Constraint?
    
    
    
    //原创微博的配图
    private lazy var pictureCollectionView: LWStatusPictureCollectionView = {
        
        
        
        let collectionView = LWStatusPictureCollectionView()
        
        //  设置配图的背景色跟转发微博的背景色一致
        collectionView.backgroundColor = self.backgroundColor
        
        return collectionView
        
        
        
    }()

    
    
    //添加属性  为了给内容赋值
    var statusViewModel: LWStatusViewModel? {
        
        //set方法 重写
        didSet {
            
            
            //移除冲突约束
            originalViewBottomConstraint?.uninstall()
            
            //在这里面给子控件赋值
            if let headImageUrl = statusViewModel?.status?.user?.profile_image_url {
                
                headImageView.sd_setImageWithURL(NSURL(string: headImageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
                
                
            }
            
            
            //昵称
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            
            // 获取时间
            timeLabel.text = statusViewModel?.timeStr
            
            //来源
            sourceLabel.text = statusViewModel?.sourceContent
            
            //认证等级
            verifiedTypeImageView.image = statusViewModel?.verified_typeImage
            
            //会员等级图片
            mbrankImageView.image = statusViewModel?.vipMbrankImage
            
            
            
            
            //设置微博类容
            contentLabel.text = statusViewModel?.status?.text
            

            
            //判断是否有配图
            if let count = statusViewModel?.status?.pic_urls?.count where count > 0 {
                
                //赋值
                pictureCollectionView.pic_urls = statusViewModel?.status?.pic_urls
                
                
                pictureCollectionView.hidden = false

                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.originalViewBottomConstraint = make.bottom.equalTo(pictureCollectionView).offset(HomeCellMargin).constraint
                    
                    
                })
                
                
                
                
            }else {
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    pictureCollectionView.hidden = true
                    
                    self.originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                    
                    
                })
                
                
                
                
            }
            

            
            
        }
        
        
        
    }
    
    
   
    
    //MARK: - 计算时间
   //逻辑放到ViewModel中
    
    
    
    
    
    
    //MARK: - 懒加载
    //头像
    private lazy var headImageView: UIImageView = {
    
        let headImage = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //倒圆角  这个有点耗性能
//        headImage.layer.cornerRadius = 18
//        headImage.layer.masksToBounds = true
        
        return headImage
    
    }()
    
    
    //昵称  调用UIlabel的分类方法
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), titleFont: 15)
    
    //认证类型等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), titleFont: 12)
    
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), titleFont: 12)
    
    //微博内容
    private lazy var contentLabel: UILabel = {
       
        let contentLabel = UILabel(textColor: .darkGrayColor(), titleFont: 15)
        
        //换行
        contentLabel.numberOfLines = 0
        
        
        return contentLabel
        
    }()
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //设置背景色  区别cell的背景色
        backgroundColor = UIColor.whiteColor()
        
        
        
        //布局子控件
        layoutUI()
        
        
    }

    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    //MARK: - 约束
    private func layoutUI() {
        
        addSubview(headImageView)
        addSubview(screenNameLabel)
        addSubview(verifiedTypeImageView)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureCollectionView)
        
        //布局 约束
        
        //头像
        headImageView.snp_makeConstraints { (make) -> Void in
            
            
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            
            make.size.equalTo(CGSize(width: 36, height: 36))
            
            
        }
        
        //昵称
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            
           make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
            
            make.top.equalTo(headImageView)
            
            
        }
        
        
        //认证等级
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(headImageView.snp_trailing)
            
            make.centerY.equalTo(headImageView.snp_bottom)
            
            
            
        }
        
        
        //会员等级
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HomeCellMargin)
            
            make.top.equalTo(screenNameLabel)
            
            
        }
        
        
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(screenNameLabel)
            
            
        }
        
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
            
            make.bottom.equalTo(timeLabel)
            
        }
        
        
        //内容约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            
            make.top.equalTo(headImageView.snp_bottom).offset(HomeCellMargin)
            
            make.leading.equalTo(headImageView)
            
            make.trailing.equalTo(self).offset(-HomeCellMargin)
            
        }
        
        
        //配图约束
        pictureCollectionView.snp_makeConstraints { (make) -> Void in
            
            
            make.leading.equalTo(contentLabel)
            
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
            
            
        }
        
        
        
        
        //设置当前视图的约束  目的是为了自动确认当前视图的高度
        self.snp_makeConstraints { (make) -> Void in
            
            self.originalViewBottomConstraint = make.bottom.equalTo(pictureCollectionView).offset(HomeCellMargin).constraint

            
        }
        
        
        
        
    }
    
    
    
    
    
}
